# Part of rabbitears See LICENSE for permissions
# Copyright (C) 2022 Matt Arnold
from IRC import *
import os
import random
import ssl
import socket
import sys
import irctokens
import json
import sqlite3
import logging
from daemonize import Daemonize

logging.basicConfig(filename='bot.log', encoding='utf-8', level=logging.DEBUG)

class NullDevice:
    def write(self,s):
        pass

LINEEND = '\r\n'
# IRC Config
config = None
with open('config.json') as f:
    jld = f.read()
    config = json.loads(jld)




# Need to pass the IRCBot class a socket the reason it doesn't do this itself is 
# so you can set up TLS or not as you need it
# These provide good defaults. But your milage may vary
def do_connect():
    oursock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    context = ssl.SSLContext()
    context.check_hostname = False
    context.verify_mode = ssl.CERT_NONE
    oursock = context.wrap_socket(oursock, server_hostname=config['hostname'])

    irc = IRCBot(oursock, isBad=config["isBad"])
    irc.connect(config['hostname'],
        config['port'],
        config['channel'],
        config['nick'],
        config['nickpass'])
    return irc

def hup_handle(sig, fr):
    sys.exit()

def do_mean():
    con = sqlite3.connect(config['db'])
    cur = con.cursor()
    qr = cur.execute("SELECT count(*) from mean").fetchone()
    maxrows = int(qr[0])
    slrow = str(random.randint(1, maxrows))
    qr = cur.execute("SELECT data from mean where rowid=(?)", (slrow,)).fetchone()
    result = str(qr[0])
    return result

def generate_response(person, message):
    msg = message.strip(LINEEND)
    if 'cool.person' in person and msg.lower() == "hello botley":
        return "Greetings Master"
    elif msg.lower() == "hello":
        return "Greetings Human!"
    elif "Ground Control to Major Tom" in msg:
        return "Put your helmet on!"
    elif "Ziggy Stardust" in msg:
        return "Looks good in leather pants"
    elif msg.lower() == "!mean":
      return  do_mean()
    else:
        return None

def do_main_loop():
    irc = do_connect()
    while True:
        try:

            text = irc.get_response()
            logging.debug(text[0],text[1],text[2])
            if text[1] == "MODE": # in leiu of RPL_WELCOME
                botmode = irctokens.build("MODE", [config['nick'], '+B'])
                irc.send_cmd(botmode)
            if text[1] == 'PRIVMSG' and text[2][0] == config['channel']:
                r = generate_response(text[0],text[2][1])
                if r is not None:
                    irc.send_privmsg(config['channel'],r)
        except IRCError as e:
            logging.error(e)
            sys.exit(1)

pid = "bot.pid"
daemon = Daemonize(app="theodebot", pid=pid, action=do_main_loop)
daemon.start()